<?php

class ScaffolderGenerator {
  
  const SCAFFOLDER_SUBDIR = "scaffolders/";
  
  public static function materialize( Scaffoldable $pSf, $pName, $pPath ) {
    //if ( $pSf instanceof ScaffoldableParent  ) return self::materializeScaffoldableParent($pSf, $pName, $pPath);
    return self::materializeScaffoldable($pSf, $pName, $pPath);
  }
  
  
  public static function getSmarty() {
    $smarty = new SmartyExt();
    // set custom delimeters to avoid using {literal} a lot
    $smarty->right_delimiter = ":]";
    $smarty->left_delimiter = "[:"; 
    $smarty->template_dir = dirname(__FILE__)."/new_sf_template/";
    return $smarty;
  }
  
  public static function materializeScaffoldable( Scaffoldable $pSf, $pName, $pPath ) {
    $smarty = self::getSmarty();
    $smarty->assign('newSfClassName', $pName);
    $smarty->assign('newSfDir', $pPath);
    $classCont = $smarty->fetch('scaffolder.dry');
    
    //check if project exists:
    if (!is_dir($pPath)) die("Project $pPath doesn't exist.\n");
    if (!is_writable($pPath)) die("Can't write into $pPath \n");
    
    $scaffPath = $pPath . self::SCAFFOLDER_SUBDIR.$pName."/";
    
    //save generated class:
    if (!is_dir($scaffPath)) mkdir($scaffPath);
    file_put_contents($scaffPath.$pName.".php", $classCont);
    
    $tplsDir = $scaffPath. "/tpls/";
    if (!is_dir($tplsDir)) mkdir($tplsDir);
    file_put_contents($tplsDir. 'container.html' , $pSf->getContainerTpl());
    file_put_contents($tplsDir. 'single_emit.html' , $pSf->getSingleEmitTpl());    
  }
  
  
  public static function materializeScaffoldableParent( ScaffoldableParent $pSf, $pName, $pPath ) {
    throw new Exception("Not yet implemented!");
  }
  
}